package com.example.myandroidapp1;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MyGFXSurfaceView extends SurfaceView implements Runnable {

	private SurfaceHolder ourHolder;
	private Thread ourThread;
	private boolean isRunning = false;

	private float x;
	private float y;
	private Bitmap testBitMap, plusBitMap;
	private float sX, sY, fX, fY;
	private float animX,animY,dX,dY,scaledX,scaledY;
	private float sensorX, sensorY;

	public float getSensorX() {
		return sensorX;
	}

	public void setSensorX(float sensorX) {
		this.sensorX = sensorX;
	}

	public float getSensorY() {
		return sensorY;
	}

	public void setSensorY(float sensorY) {
		this.sensorY = sensorY;
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public MyGFXSurfaceView(Context context) {
		super(context);
		sX = 0;
		sY = 0;
		fX = 0;
		fY = 0;
		x = 0;
		y = 0;
		animX = animY = dX = dY = scaledX = scaledY = 0;
		testBitMap = BitmapFactory.decodeResource(getResources(),
				R.drawable.green_ball);
		plusBitMap = BitmapFactory.decodeResource(getResources(),
				R.drawable.plus_green);

		ourHolder = getHolder();
	}

	public float getAnimX() {
		return animX;
	}

	public void setAnimX(float animX) {
		this.animX = animX;
	}

	public float getAnimY() {
		return animY;
	}

	public void setAnimY(float animY) {
		this.animY = animY;
	}

	public float getdX() {
		return dX;
	}

	public void setdX(float dX) {
		this.dX = dX;
	}

	public float getdY() {
		return dY;
	}

	public void setdY(float dY) {
		this.dY = dY;
	}

	public float getScaledX() {
		return scaledX;
	}

	public void setScaledX(float scaledX) {
		this.scaledX = scaledX;
	}

	public float getScaledY() {
		return scaledY;
	}

	public void setScaledY(float scaledY) {
		this.scaledY = scaledY;
	}

	public float getsX() {
		return sX;
	}

	public void setsX(float sX) {
		this.sX = sX;
	}

	public float getsY() {
		return sY;
	}

	public void setsY(float sY) {
		this.sY = sY;
	}

	public float getfX() {
		return fX;
	}

	public void setfX(float fX) {
		this.fX = fX;
	}

	public float getfY() {
		return fY;
	}

	public void setfY(float fY) {
		this.fY = fY;
	}

	public void pause() {
		isRunning = false;
		while (true) {
			try {
				ourThread.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			break;
		}
		ourThread = null;
	}

	public void resume() {
		isRunning = true;
		ourThread = new Thread(this);
		ourThread.start();
	}

	@Override
	public void run() {
		while (isRunning) {
			if (!ourHolder.getSurface().isValid()) {
				continue;
			}

			Canvas canvas = ourHolder.lockCanvas();
			canvas.drawColor(Color.WHITE);
			/*if (getX() != 0 && getY() != 0) {
				canvas.drawBitmap(testBitMap, getX() - testBitMap.getWidth()
						/ 2, getY() - testBitMap.getHeight() / 2, null);
			}
			if (getsX() != 0 && getsY() != 0) {
				canvas.drawBitmap(plusBitMap, getsX() - plusBitMap.getWidth()
						/ 2, getsY() - plusBitMap.getHeight() / 2, null);
			}
			if (getfX() != 0 && getfY() != 0) {
				canvas.drawBitmap(testBitMap, getfX() - testBitMap.getWidth()
						/ 2 - getAnimX(), getfY() - testBitMap.getHeight() / 2 - getAnimY(), null);
		
				canvas.drawBitmap(plusBitMap, getfX() - plusBitMap.getWidth()
						/ 2, getfY() - plusBitMap.getHeight() / 2, null);
			}
			
			setAnimX(getAnimX() + getScaledX());
			setAnimY(getAnimY() + getScaledY());*/
			
			float centerX = canvas.getWidth()/2;
			float centerY = canvas.getHeight()/2;
			canvas.drawBitmap(testBitMap, centerX+sensorX*250, centerY + sensorY*250,null);
			
			
			ourHolder.unlockCanvasAndPost(canvas);
		}

	}

	public Bitmap getTestBitMap() {
		return testBitMap;
	}

	public void setTestBitMap(Bitmap testBitMap) {
		this.testBitMap = testBitMap;
	}

	public Bitmap getPlusBitMap() {
		return plusBitMap;
	}

	public void setPlusBitMap(Bitmap plusBitMap) {
		this.plusBitMap = plusBitMap;
	}

}
